package com.wangmin.intercept.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.wangmin.intercept.R;
import com.wangmin.intercept.helper.SnackBarHelper;
import com.wangmin.intercept.manager.LocalActivityManager;
import com.wangmin.intercept.manager.SystemBarTintManager;
import com.wangmin.intercept.utils.LogUtils;
import com.wangmin.intercept.utils.ViewUtils;

/**
 * Created by wm on 2017/4/
 */
public class BaseActivity extends AppCompatActivity{
    protected LogUtils logUtils;
    protected SystemBarTintManager tintManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LocalActivityManager.getInstance().addActivity(this);
        ViewUtils.setTranslucentStatus(true, this);
        tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
//        if(ViewUtils.checkDeviceHasNavigationBar(SdkInit.getContext())) {
        tintManager.setNavigationBarTintEnabled(true);
//        }

        // 自定义颜色
        try {
            int color = getResources().getColor(R.color.colorPrimary);
            tintManager.setStatusBarTintColor(color);
        } catch (Exception e) {
            e.printStackTrace();
        }
        logUtils = LogUtils.getNewInstance(this);
    }

    public void startActivity(Class<?> clazz, Bundle bundle) {
        Intent intent = new Intent(this, clazz);
        if (bundle != null) {
            intent.putExtra("bundle", bundle);
        }
        startActivity(intent);
        overridePendingTransition(R.anim.anim_activity_in_right, R.anim.anim_activity_out_right);

    }

    public void finishWithAnimation() {
        finish();
        overridePendingTransition(R.anim.anim_activity_in_left, R.anim.anim_activity_out_left);
    }

    public void finishSearchAnimation() {
        finish();
        overridePendingTransition(R.anim.anim_activity_search_in, R.anim.anim_activity_search_out);
    }

    public void startSearchActivity(Class<?> clazz, Bundle bundle) {
        Intent intent = new Intent(this, clazz);
        if (bundle != null) {
            intent.putExtra("bundle", bundle);
        }
        startActivity(intent);
        overridePendingTransition(R.anim.anim_activity_search_in, R.anim.anim_activity_search_out);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finishWithAnimation();
        }
        return true;
    }

    @Override
    public void onBackPressed() {
        finishWithAnimation();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LocalActivityManager.getInstance().removeActivity(this);
    }

    /**
     * 显示toast
     *
     * @param msg
     */
    public void showToastMsg(String msg) {
        Snackbar snackbar = Snackbar.make(getWindow().getDecorView(), msg, Snackbar.LENGTH_SHORT);
        SnackBarHelper.alertNomal(snackbar).show();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {

            // 获得当前得到焦点的View，一般情况下就是EditText（特殊情况就是轨迹求或者实体案件会移动焦点）
            View v = getCurrentFocus();

            if (isShouldHideInput(v, ev)) {
                hideSoftInput(v.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 根据EditText所在坐标和用户点击的坐标相对比，来判断是否隐藏键盘，因为当用户点击EditText时没必要隐藏
     *
     * @param v
     * @param event
     * @return
     */
    private boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left
                    + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击EditText的事件，忽略它。
                return false;
            } else {
                return true;
            }
        }
        // 如果焦点不是EditText则忽略，这个发生在视图刚绘制完，第一个焦点不在EditView上，和用户用轨迹球选择其他的焦点
        return false;
    }

    /**
     * 多种隐藏软件盘方法的其中一种
     *
     * @param token
     */
    private void hideSoftInput(IBinder token) {
        if (token != null) {
            InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            im.hideSoftInputFromWindow(token,
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}
